﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Web.UI;
using NickLee.Common;
using System.Text;
using GPCSC.Web.sjsbyes;

namespace GPCSC.Web.CM
{
    public partial class SelData :  PageBase
    {
        public double[] ad = new double[26];

        public string add = "0";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
            }
        }

        #region
        protected void sumss(DataSet dsname, string sql, string kmonth, string jmonth,DataTable DT,int year)
        {

           

            if (dsname != null)
            {
                if (dsname.Tables.Count > 0)
                {
                    if (dsname.Tables[0].Rows.Count > 0)
                    {
                      
                      
                        //循环遍历每个领导人
                        foreach (DataRow dr in dsname.Tables[0].Rows)
                        {
                            DataRow drr = DT.NewRow();
                            string userid = dr["CL_USER_SOID"].ToString();
                            Hashtable htables = new Hashtable();
                            htables.Add("@lgid", userid);
                            htables.Add("@year", year);
                            htables.Add("@kmonth", kmonth);
                            htables.Add("@jmonth", jmonth);
                            DataSet ds = new SqlReadXml().ReaderDataSet(sql, htables);

                            if (ds != null)
                            {
                                if (ds.Tables.Count > 0)
                                {
                                    if (ds.Tables[0].Rows.Count > 0)
                                    {
                                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                        {
                                            ad[0] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["zday"].ToString());//下基层活动天数
                                            ad[1] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XJCDYTS"].ToString());//下基层调研
                                            ad[2] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZXMQRJPS"].ToString());//民情
                                            ad[3] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZCRHTS"].ToString());//走村入户
                                            ad[4] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDZHTS"].ToString());//驻点住户
                                            ad[5] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["GKJFTS"].ToString());//公开接访天数
                                            ad[6] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["JDXFAJ"].ToString());//接待信访件数
                                            ad[7] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["yjaxf"].ToString());//已结案
                                            ad[8] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJS"].ToString());//重点信访
                                            ad[9] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJWCS"].ToString());//重点信访包案。。
                                            ad[10] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["KNQZZHS"].ToString());//已走访困难
                                            ad[11] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWKNQZQK_YZFHS"].ToString());//已走访挂钩。。
                                            ad[12] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["YBFHS"].ToString());// 已帮扶挂钩。。
                                            ad[13] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWJZZHLJJE"].ToString());//慰问捐助金额。。
                                            ad[14] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZKZTHCS"].ToString());
                                            ad[15] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["Z_CJRC"].ToString());
                                            ad[16] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XSZCXJCS"].ToString());
                                            ad[17] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["X_CJRC"].ToString());
                                            ad[18] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["CJLDTS"].ToString());
                                            ad[19] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WJCDWFCZJLJJE"].ToString());
                                            ad[25] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["dybg"].ToString());
                                        }
                                        
                                    }
                                    else
                                    {
                                        for (int m = 0; m < 20; m++)
                                        {

                                            ad[m] = 0;
                                           
                                        }
                                        ad[25] = 0;
                                       
                                    }
                                }
                            }
                            adwt(dr["CL_USER_SOID"].ToString(), kmonth, jmonth, year);//问题总计
                            jjwt(dr["CL_USER_SOID"].ToString(), kmonth, jmonth, year);//解决问题统计
                            Bss(dr["CL_USER_SOID"].ToString(), kmonth, jmonth, year);//办实事
                            knAgg(dr["CL_USER_SOID"].ToString());
                            drr["zday"] = ad[0];
                            drr["XJCDYTS"] = ad[1];
                            drr["ZXMQRJPS"] = ad[2];
                            drr["ZCRHTS"] = ad[3];
                            drr["ZDZHTS"] = ad[4];
                            drr["GKJFTS"] = ad[5];
                            drr["JDXFAJ"] = ad[6];
                            drr["yjaxf"] = ad[7];
                            drr["ZDXFBAJS"] = ad[8];
                            drr["ZDXFBAJWCS"] = ad[9];
                            drr["KNQZZHS"] = ad[10];
                            drr["WWKNQZQK_YZFHS"] = ad[11];
                            drr["YBFHS"] = ad[12];
                            drr["WWJZZHLJJE"] = ad[13];

                            drr["ZKZTHCS"] = ad[14];
                            drr["Z_CJRC"] = ad[15];
                            drr["XSZCXJCS"] = ad[16];
                            drr["X_CJRC"] = ad[17];
                            drr["CJLDTS"] = ad[18];
                            drr["WJCDWFCZJLJJE"] = ad[19];
                            drr["dybg"] = ad[25];
                            drr["wts"] = ad[20];
                            drr["jjs"] = ad[21];
                            drr["sss"] = ad[22];
                            drr["kns"] = ad[23];
                            drr["ggs"] = ad[24];
                            drr["PostName"] = dr["POSTName"].ToString();
                            drr["UserName"] = dr["USERNAME"].ToString();
                            DT.Rows.Add(drr);
                            for (int i = 0; i < 26; i++)
                            {
                                ad[i] = 0;
                            }
                        }
                    }
                }
            }
         
        }

        // 问题统计
        protected void adwt(string userid, string kmonth, string jmonth,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countWT", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["wts"].ToString());
            }
            ad[20] = wts ;
           
        }

        //解决问题统计
        protected void jjwt(string userid, string kmonth, string jmonth,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countjjWT", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            ad[21]= wts;
          
        }
        //办实事情况
        protected void Bss(string userid, string kmonth, string jmonth,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("counBss", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            ad[22]= wts;
            
        }

        //困难及挂钩结对户数
        protected void knAgg(string userid)
        {
            int kns = 0;
            int ggs = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            DataSet dwts = new SqlReadXml().ReaderDataSet("selks", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {

                foreach (DataRow dr in dwts.Tables[0].Rows)
                {
                    kns += Usual.f_convertInt32(dr["knqzzhs"].ToString());
                    ggs += Usual.f_convertInt32(dr["ggjdhs"].ToString());

                }
            }
            ad[23]= kns;
            ad[24]= ggs;


        }
        #endregion

        protected void LCallBack_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
            try
            {
                DataTable DT = new DataTable();
                DT.Columns.Add("PostName");
                DT.Columns.Add("UserName");
                DT.Columns.Add("zday",typeof(double));
                DT.Columns.Add("XJCDYTS", typeof(double));
                DT.Columns.Add("ZXMQRJPS", typeof(double));
                DT.Columns.Add("ZCRHTS",typeof(double));
                DT.Columns.Add("ZDZHTS", typeof(double));
                DT.Columns.Add("GKJFTS", typeof(double));
                DT.Columns.Add("JDXFAJ", typeof(double));
                DT.Columns.Add("yjaxf", typeof(double));
                DT.Columns.Add("ZDXFBAJS", typeof(double));
                DT.Columns.Add("ZDXFBAJWCS", typeof(double));
                DT.Columns.Add("KNQZZHS", typeof(double));
                DT.Columns.Add("WWKNQZQK_YZFHS", typeof(double));
                DT.Columns.Add("YBFHS", typeof(double));
                DT.Columns.Add("WWJZZHLJJE", typeof(double));
                DT.Columns.Add("ZKZTHCS", typeof(double));
                DT.Columns.Add("Z_CJRC", typeof(double));
                DT.Columns.Add("XSZCXJCS", typeof(double));
                DT.Columns.Add("X_CJRC", typeof(double));
                DT.Columns.Add("CJLDTS", typeof(double));
                DT.Columns.Add("WJCDWFCZJLJJE", typeof(double));
                DT.Columns.Add("wts", typeof(double));
                DT.Columns.Add("jjs", typeof(double));
                DT.Columns.Add("sss", typeof(double));
                DT.Columns.Add("kns", typeof(double));
                DT.Columns.Add("ggs", typeof(double));
                DT.Columns.Add("dybg", typeof(double));
                DataSet dsU = new SqlReadXml().ReaderDataSet("sel_user_all");

                string kdate = Usual.f_convertDateTime(e.Parameters[0].ToString()).Month.ToString();
                string jdate = Usual.f_convertDateTime(e.Parameters[1].ToString()).Month.ToString();
                int year = Usual.f_convertDateTime(e.Parameters[1].ToString()).Year;



                sumss(dsU, "newSelAllbyuserid_km", kdate, jdate, DT, year);


                string sqls = e.Parameters[2].ToString();
                DataTable dts = DT.Clone();
                DataRow[] rows = DT.Select(e.Parameters[2].ToString());
                foreach (DataRow row in rows)  // 将查询的结果添加到dt中； 
                {
                    dts.Rows.Add(row.ItemArray);
                }


                Repeater1.DataSource = dts;
                Repeater1.DataBind();

            }
            catch (Exception ee)
            {
                throw ee;
            }
            finally
            {
                LPanel.RenderControl(e.Output);
            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            DataTable DT = new DataTable();
            DT.Columns.Add("PostName");
            DT.Columns.Add("UserName");
            DT.Columns.Add("zday");
            DT.Columns.Add("XJCDYTS");
            DT.Columns.Add("ZXMQRJPS");
            DT.Columns.Add("ZCRHTS");
            DT.Columns.Add("ZDZHTS");
            DT.Columns.Add("GKJFTS");
            DT.Columns.Add("JDXFAJ");
            DT.Columns.Add("yjaxf");
            DT.Columns.Add("ZDXFBAJS");
            DT.Columns.Add("ZDXFBAJWCS");
            DT.Columns.Add("KNQZZHS");
            DT.Columns.Add("WWKNQZQK_YZFHS");
            DT.Columns.Add("YBFHS");
            DT.Columns.Add("WWJZZHLJJE");
            DT.Columns.Add("ZKZTHCS");
            DT.Columns.Add("Z_CJRC");
            DT.Columns.Add("XSZCXJCS");
            DT.Columns.Add("X_CJRC");
            DT.Columns.Add("CJLDTS");
            DT.Columns.Add("WJCDWFCZJLJJE");
            DT.Columns.Add("wts");
            DT.Columns.Add("jjs");
            DT.Columns.Add("sss");
            DT.Columns.Add("kns");
            DT.Columns.Add("ggs");
            DT.Columns.Add("dybg");
            string likes = this.dw.Value.Trim();
            if (likes != "")
            {
                for (int i = 0; i < 26; i++)
                {
                    ad[i] = 0;
                }
                likes = "%" + likes + "%";
                Hashtable htab = new Hashtable();
                htab.Add("@like", likes);
                 DataSet ds = new DataSet();
                if (DropDownList1.SelectedValue == "post")
                {
                      ds = new SqlReadXml().ReaderDataSet("sel_POST_bylike", htab);
                    if(ds.Tables.Count>0)
                    {
                        if(ds.Tables[0].Rows.Count>0)
                        {
                            foreach(DataRow dr in ds.Tables[0].Rows)
                            {
                                DataSet dsu = new Summ().PostUser(dr["CL_POST_SOID"].ToString());
                                sumss(dsu, "newSelAllbyuserid_km", "1", DateTime.Now.Month.ToString(),DT,DateTime.Now.Year);

                            }
                        }
                    }
                }
                else if(DropDownList1.SelectedValue=="name") 
                {

                    DataSet dsU = new SqlReadXml().ReaderDataSet("sel_user_bylike", htab);
                    sumss(dsU, "newSelAllbyuserid_km", "1", DateTime.Now.Month.ToString(), DT, DateTime.Now.Year);
                }
            }
            Repeater1.DataSource = DT;
            Repeater1.DataBind();
        }
     

        protected void Button2_Click(object sender, EventArgs e)
        {
            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
            string filename = "淮安市" + DateTime.Now.Year.ToString() + "年" + "三进三帮统计表.doc";
            Response.Clear();
            Response.BufferOutput = true;
            //设定输出的字符集
            Response.Charset = "GB2312";
            //假定导出的文件名为FileName.doc
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            /**/
            ////设置导出文件的格式
            Response.ContentType = "application/ms-word";
            //关闭ViewState


            // GrwResult.EnableViewState = false;
            LPanel.EnableViewState = false;


            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

            LPanel.RenderControl(textWriter);
            //   GrwResult.RenderControl(textWriter);

            //SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
            //Hashtable htable = new Hashtable();
            //htable.Add("fileUrl", stringWriter.ToString());

            //#region  首先验证该月是否有 记录

            //string year = DateTime.Now.Year.ToString();
            //string month = DateTime.Now.Month.ToString();
            //Hashtable htab = new Hashtable();
            //htab.Add("@year", year);
            //htab.Add("@month", month);
            ////htab.Add("@POstID", users.CL_POSTID_PK.ToString());
            //DataSet ds = new SqlReadXml().ReaderDataSet("tifileds", htab);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
            //    dtable.databyhtables(htable, "update", "TJfile", "FileID", ds.Tables[0].Rows[0]["FileID"].ToString());
            //}
            //else
            //{
            //    htable.Add("filename", filename);
            //    htable.Add("TJdate", DateTime.Now);
            //    htable.Add("POstID", users.CL_POSTID_PK.ToString());
            //    htable.Add("PostName", users.POSTName.ToString());
            //    htable.Add("year", DateTime.Now.Year.ToString());
            //    htable.Add("month", DateTime.Now.Month.ToString());
            //    htable.Add("cj",1);
            //    dtable.databyhtables(htable, "add", "TJfile", "", "");
            //}
            //#endregion

            // //把HTML写回浏览器
            Response.Write(stringWriter.ToString());
            Response.End();


        }
    }
}